//
//  YYT_YuedanLeftViewController.m
//  AudioVisualFeast
//
//  Created by 张琪 on 16/2/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "QL_YuedanLeftViewController.h"
#import "QL_AFNTools.h"
#import <MJRefresh.h>
#import "QL_YuedanLeftModel.h"
#import "QL_YuedanLeftTableViewCell.h"
#import "QL_MVPlayerViewController.h"

@interface QL_YuedanLeftViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain) NSMutableArray *yuedanDbArray;
@property (nonatomic,retain) UISegmentedControl *segment;

@property (nonatomic,assign) NSInteger num;
@property (nonatomic,retain) NSString *area;
@property (nonatomic,retain) UITableView *tableview;
@property (nonatomic,copy) NSString *play;
@property (nonatomic,copy) NSString *parameter;
@end

@implementation QL_YuedanLeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.yuedanDbArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor blackColor];
    self.navigationItem.title = @"悦单";
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"MVfanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];

    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    NSArray *array = [[NSArray alloc]initWithObjects:@"1",@"2",@"3", nil];
    self.segment = [[UISegmentedControl alloc]initWithItems:array];
    self.segment.frame = CGRectMake(20, 0, WIDTH- 40, 30);
    [self.view addSubview:self.segment];
    
    self.segment.selectedSegmentIndex= 0;
    [self.segment setTitle:@"精选" forSegmentAtIndex:0];
    [self.segment setTitle:@"热门" forSegmentAtIndex:1];
    [self.segment setTitle:@"最新" forSegmentAtIndex:2];
    
    self.segment.tintColor = [UIColor colorWithRed:30/255.0 green:195/255.0 blue:153/255.0 alpha:1.0];
    [self.segment addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    
    self.area = @"CHOICE";
    self.num = 0;
    [self getData:self.area with:self.num];
    
    [self.view addSubview:self.tableview];
    
    [self addHeader];
    [self addFooter];
    
}

- (void)leftAction:(UIBarButtonItem *)btn{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

- (void)getData:(NSString *)data with:(NSInteger)dataNum{
    [QL_AFNTools getWithUrl:@"http://mapi.yinyuetai.com/playlist/list.json?D-A=0" parameter:@{@"category" :data ,@"offset":[NSString stringWithFormat:@"%ld", dataNum], @"size":@"20"} httpHeader:@{@"App-Id":@"10101026",@"Device-Id":@"587d897556663eb999e31a4c621abfbc",@"Device-V":@"aVBob25lIE9TXzkuMl82NDAqMTEzNl8xMDAwMDEwMDBfaVBob25lNiwy"} resposeType:ResponseTypeJSON success:^(id result) {
        NSDictionary *dic = result;
        NSMutableArray *array = [dic objectForKey:@"playLists"];
        for (NSDictionary *dics in array) {
            QL_YuedanLeftModel *yuedanlModel = [[QL_YuedanLeftModel alloc]initWithDictionary:dics];
            [self.yuedanDbArray addObject:yuedanlModel];
            
        }
        [_tableview reloadData];
        
    } fail:^(NSError *error) {
        
        
    }];
}

- (void)getData1:(NSString *)canshu{
    [QL_AFNTools getWithUrl:@"http://mapi.yinyuetai.com/playlist/show.json?D-A=0" parameter:@{@"id":canshu} httpHeader:@{@"App-Id":@"10101026",@"Device-Id":@"587d897556663eb999e31a4c621abfbc",@"Device-V":@"aVBob25lIE9TXzkuMl82NDAqMTEzNl8xMDAwMDEwMDBfaVBob25lNiwy"} resposeType:ResponseTypeJSON success:^(id result) {
        NSDictionary *dic = result;
        NSNumber *z = [dic objectForKey:@"videos"][0][@"id"];
        [self getDataUrl:[NSString stringWithFormat:@"%@",z]];
        
    } fail:^(NSError *error) {
        
        
    }];
}

- (void)getDataUrl:(NSString *)str{
    [QL_AFNTools getWithUrl:@"http://mapi.yinyuetai.com/video/show.json?D-A=0" parameter:@{@"id":str,@"relatedVideos":@"true",@"supportHtml":@"true",@"v":@"2"} httpHeader:@{@"App-Id":@"10101026",@"Device-Id":@"587d897556663eb999e31a4c621abfbc",@"Device-V":@"aVBob25lIE9TXzkuMl82NDAqMTEzNl8xMDAwMDEwMDBfaVBob25lNiwy"} resposeType:ResponseTypeJSON success:^(id result) {
        NSDictionary *dic = result;
        self.play = [dic objectForKey:@"hdUrl"];
        
    } fail:^(NSError *error) {
        
        
    }];
}

- (UITableView *)tableview{
    if (!_tableview) {
        self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
        [_tableview registerClass:[QL_YuedanLeftTableViewCell class] forCellReuseIdentifier:@"yuedanLCell"];
        _tableview.backgroundColor = [UIColor blackColor];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    }return _tableview;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _yuedanDbArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 190;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QL_YuedanLeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"yuedanLCell"];
    cell.model = [self.yuedanDbArray objectAtIndex:indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.parameter = [NSString stringWithFormat:@"%@",[self.yuedanDbArray[indexPath.row] ids]];
    [self getData1:self.parameter];
    
    QL_MVPlayerViewController *mv = [[QL_MVPlayerViewController alloc]init];
    mv.playurl = self.play;
    mv.headtitle = [self.yuedanDbArray[indexPath.row] title];
    mv.ids = [NSString stringWithFormat:@"%@",[self.yuedanDbArray[indexPath.row] ids]];
    mv.pic = [self.yuedanDbArray[indexPath.row]playListBigPic];
    mv.typeup = 1;
    [self.navigationController pushViewController:mv animated:NO];
}
//segment点击事件
- (void)segAction:(UISegmentedControl *)seg{
    if (seg.selectedSegmentIndex == 0) {
        [self.yuedanDbArray removeAllObjects];
        self.area = @"CHOICE";
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self getData:self.area with:self.num];
            
        });
    }else if (seg.selectedSegmentIndex == 1){
        [self.yuedanDbArray removeAllObjects];
        self.area = @"HOT";
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self getData:self.area with:self.num];
            
        });
    }else if (seg.selectedSegmentIndex == 2){
        [self.yuedanDbArray removeAllObjects];
        self.area = @"NEW";
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self getData:self.area with:self.num];
            
        });
    }
}
//上拉
- (void)addFooter{
    self.tableview.mj_footer
     = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.num += 20;
            [self getData:self.area with:self.num];
            [self.tableview.mj_footer endRefreshing];
            
        });
         
     }];
}
//下拉
- (void)addHeader{
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.num = 0;
//            [self.yuedanDbArray removeAllObjects];
            [self getData:self.area with:self.num];
            [self.tableview.mj_header endRefreshing];
            
        });
       
    }];
    [self.tableview.mj_header beginRefreshing];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
